package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.data.DataBase;
import com.entity.User;

@WebServlet(urlPatterns={"/register"})
public class RegisterServlet extends HttpServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		PrintWriter out = resp.getWriter();
		
		//获得参数
		String username=req.getParameter("username");
		String password=req.getParameter("password");
		String address=req.getParameter("address");
		if(!DataBase.data.containsKey(username)){
			//用户名没有注册
			User user=new User(username, password, address);
			DataBase.data.put(username, user);
			out.print("1");

		}
		else{
			//用户名存在
			out.print("0");
		}
	}
}
